package translinkApp.DialogBoxes.SaveJourney;

import translinkApp.Activities.R;
import translinkApp.Data.DataTypes.Date;
import translinkApp.Data.DataTypes.Time;
import translinkApp.Data.Journey.Search.JourneySearch;
import translinkApp.Data.Journey.Search.JourneySearch.TimeType;
import translinkApp.Data.Journey.Search.JourneySearch.TransportType;
import translinkApp.Data.ResolvedLocation.ResolvedLocation;
import translinkApp.Utilities.DataResource;
import translinkApp.views.dialog.LachDialog;
import translinkApp.views.dialog.LachDialogArguments;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class SaveJourneyDialog extends LachDialog<SaveJourneyDialogData> {

	private Context context;
	private JourneySearch searchInformation;

	private EditText journeyName;
	private CheckBox fromLocation;
	private CheckBox toLocation;
	private CheckBox leaveType;
	private CheckBox transportType;
	private CheckBox dateCheckBox;
	private CheckBox timeCheckBox;
	private TimeType leaveTypeVal;
	private TransportType transportTypeVal;

	private Button saveButton;
	private Button cancelButton;

	public SaveJourneyDialog(final Context context, JourneySearch searchInformation,
			LachDialogArguments args, SaveJourneyDialogData saveDialogData) {

		super(context, R.layout.savejourneydialog, args, saveDialogData);
		this.context = context;
		this.searchInformation = searchInformation;
		this.leaveTypeVal = (TimeType) args.getArg(0);
		this.transportTypeVal = (TransportType) args.getArg(1);

		setTitle("Save Favourite Trip                    ");

		journeyName = (EditText) findViewById(R.id.journeyNameText);
		fromLocation = (CheckBox) findViewById(R.id.saveJourneyFromLocation);
		toLocation = (CheckBox) findViewById(R.id.saveJourneyToLocation);
		leaveType = (CheckBox) findViewById(R.id.saveJourneyLeaveType);
		transportType = (CheckBox) findViewById(R.id.saveJourneyTransportType);
		dateCheckBox = (CheckBox) findViewById(R.id.saveJourneyDate);
		timeCheckBox = (CheckBox) findViewById(R.id.saveJourneyTime);

		if (saveDialogData != null) {
			loadDialogData(saveDialogData);
		}

		if (searchInformation.getFromLocation() == null) {
			fromLocation.setChecked(false);
			fromLocation.setEnabled(false);
		}

		if (searchInformation.getToLocation() == null) {
			toLocation.setChecked(false);
			toLocation.setEnabled(false);
		}

		saveButton = (Button) findViewById(R.id.saveJourneySave);
		cancelButton = (Button) findViewById(R.id.saveJourneyCancel);

		cancelButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				cancel();
			}

		});

		saveButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AddJourney();
				cancel();
			}
		});

	}

	@Override
	public void loadDialogData(SaveJourneyDialogData data) {

		fromLocation.setChecked(data.isFromLocation());
		toLocation.setChecked(data.isToLocation());
		leaveType.setChecked(data.isLeaveType());
		transportType.setChecked(data.isTransportType());
		dateCheckBox.setChecked(data.isDate());
		timeCheckBox.setChecked(data.isTime());
		journeyName.setText(data.getName());
	}

	public SaveJourneyDialogData getDialogData() {

		return new SaveJourneyDialogData(fromLocation.isChecked(), toLocation.isChecked(),
				leaveType.isChecked(), transportType.isChecked(), dateCheckBox.isChecked(),
				timeCheckBox.isChecked(), journeyName.getText().toString());

	}

	public void AddJourney() {

		ResolvedLocation fromlocation = null;
		if (fromLocation.isChecked()) {
			fromlocation = searchInformation.getFromLocation();
		}

		ResolvedLocation tolocation = null;
		if (toLocation.isChecked()) {
			tolocation = searchInformation.getToLocation();
		}

		TimeType leavetype = null;
		if (leaveType.isChecked()) {
			leavetype = leaveTypeVal;
		}

		TransportType transport = null;
		if (transportType.isChecked()) {
			transport = transportTypeVal;
		}

		Date date = null;
		if (dateCheckBox.isChecked()) {
			date = searchInformation.getDate();
		}

		Time time = null;
		if (timeCheckBox.isChecked()) {
			time = searchInformation.getTime();
		}

		JourneySearch search = null;
		try {
			search = new JourneySearch(fromlocation, tolocation, transport, leavetype, time, date);
		} catch (Exception e) {
			e.printStackTrace();
		}

		search.setJourneyName(journeyName.getText().toString());

		DataResource.journeysList.getData().add(search);
		DataResource.journeysList.save(this.context);

		Toast.makeText(this.context, "Journey added successfully.", Toast.LENGTH_SHORT).show();

	}

}